﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.DAO;
using SCRUM.Core.BusinessObjects;
using SCRUM.Core.MembershipProviders;

/// <summary>
/// Represents dropdown lists to manipulate with Project, Release and sprint hierarchy 
/// </summary>
public partial class SprintSelection : HierachicalObjectSelection
{
    /// <summary>
    /// Event triggered when sprint changes
    /// </summary>
    public event EventHandler SelectedSprintChangedEvent;

    /// <summary>
    /// Event triggered when release changes
    /// </summary>
    public event EventHandler SelectedReleaseChangedEvent;

    /// <summary>
    /// Event triggered when project changes
    /// </summary>
    public event EventHandler SelectedProjectChangedEvent;

    /// <summary>
    /// Gets the selected sprint.
    /// </summary>
    /// <value>The selected sprint.</value>
    public Sprint SelectedSprint
    {
        get
        {
            if (String.IsNullOrEmpty(this.DropDownListSprints.SelectedValue) || this.DropDownListSprints.SelectedValue == "-1")
            {
                return null;
            }
            else
            {
                return DAOSprint.GetSprintById(Int32.Parse(this.DropDownListSprints.SelectedValue));
            }
        }
    }

    /// <summary>
    /// Selected sprint ID
    /// </summary>
    public int SelectedSprintID
    {
        get
        {
            if (String.IsNullOrEmpty(this.DropDownListSprints.SelectedValue) || this.DropDownListSprints.SelectedValue == "-1")
            {
                return -1;
            }
            else
            {
                return Int32.Parse(this.DropDownListSprints.SelectedValue);
            }
        }
    }

    /// <summary>
    /// Holds selected release ID
    /// </summary>
    public int SelectedReleaseID
    {
        get
        {
            return ReleaseSelectionSprint.SelectedReleaseID;
        }
    }

    /// <summary>
    /// Holds selected projcet ID
    /// </summary>
    public int SelectedProjectID
    {
        get
        {
            return ReleaseSelectionSprint.SelectedProjectID;
        }
    }

    /// <summary>
    /// Gets the selected release.
    /// </summary>
    /// <value>The selected release.</value>
    public Release GetSelectedRelease()
    {
        return this.ReleaseSelectionSprint.SelectedRelease;
    }    

    /// <summary>
    /// Gets the selected project.
    /// </summary>
    /// <value>The selected project.</value>
    public Project GetSelectedProject()
    {
        return this.ReleaseSelectionSprint.GetSelectedProject();
    }    

    /// <summary>
    /// Sets handlers for events.
    /// </summary>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected override void OnInit(EventArgs e)
    {
        this.ReleaseSelectionSprint.SelectedReleaseChangedEvent += new EventHandler(SelectedReleaseChanged);
        this.ReleaseSelectionSprint.SelectedProjectChangedEvent += new EventHandler(SelectedProjectChanged);

        this.ReleaseSelectionSprint.SelectedReleaseChangedEvent += new EventHandler(SelectedReleaseChangedKeeper);
        this.ReleaseSelectionSprint.SelectedProjectChangedEvent += new EventHandler(SelectedProjectChangedKeeper);
    }

    /// <summary>
    /// It keeps projectselectionevent for the other selections.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedProjectChangedKeeper(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            if (this.SelectedProjectChangedEvent != null)
            {
                this.SelectedProjectChangedEvent(sender, e);
            }
        }
    }

    /// <summary>
    /// It keeps releaseselectionevent for the other selections.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedReleaseChangedKeeper(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            if (this.SelectedReleaseChangedEvent != null)
            {
                this.SelectedReleaseChangedEvent(sender, e);
            }
        }
    }

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!IsPostBack)
        {
            DataBind();
        }
    }

    /// <summary>
    /// Loads the sprints.
    /// </summary>
    protected void LoadSprints()
    {
        Release r = ReleaseSelectionSprint.SelectedRelease;
        if (r == null)
        {
            Project p = ReleaseSelectionSprint.GetSelectedProject();
            if (p == null)
            {
                DropDownListSprints.DataSource = DAOSprint.GetAllSprints();
            }
            else
            {
                DropDownListSprints.DataSource = DAOSprint.GetAllSprintsForProject(p.ID);
            }
        }
        else
        {
            DropDownListSprints.DataSource = DAOSprint.GetAllSprintsForRelease(r.ID);
        }
        DropDownListSprints.DataTextField = "Name";
        DropDownListSprints.DataValueField = "ID";
        DropDownListSprints.DataBind();
        DropDownListSprints.Items.Insert(0, TopListItemForFilter("Filters","AnySprint") );
    }

    /// <summary>
    /// Binds a data source to the invoked server control and all its child controls.
    /// </summary>
    public override void DataBind()
    {
        base.DataBind();
        ReleaseSelectionSprint.DataBind();
        LoadSprints();
    }

    /// <summary>
    /// Selected release changed.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedReleaseChanged(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            LoadSprints();
        }
    }

    /// <summary>
    /// Selected project changed.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedProjectChanged(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            LoadSprints();
        }
    }

    /// <summary>
    /// Selected sprint changed.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedSprintChanged(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            if (this.SelectedSprintChangedEvent != null)
            {
                this.SelectedSprintChangedEvent(sender, e);
            }

            if (this.SelectedSprint == null)
            {
                return;
            }

            int selectedSprintID = this.SelectedSprint.ID;

            ReleaseSelectionSprint.SetProjectReleaseBySprint(selectedSprintID);

            // Reload sprints
            LoadSprints();

            // Select the right sprint
            ListItem li = DropDownListSprints.Items.FindByValue(selectedSprintID.ToString());

            if (li != null)
            {
                li.Selected = true;
                //this.SelectedProjectID = project.ID;
            }
            else
            {
                this.DropDownListSprints.SelectedValue = "-1";
                //.SelectedProjectID = -1;
            }
            //for (int i = 0; i < this.DropDownListSprints.Items.Count; i++)
            //{
            //    if (this.DropDownListSprints.Items[i].Value == selectedSprintID.ToString())
            //    {
            //        this.DropDownListSprints.SelectedIndex = i;
            //        break;
            //    }
            //}
        }
    }

    /// <summary>
    /// Sets the right project, release and sprint in their DDLs
    /// </summary>
    /// <param name="userStoryID">ID of UserStory</param>
    public void SetProjectReleaseSprintByUserStory(int userStoryID)
    {
        Sprint sprint = DAOSprint.GetSprintByUserStoryID(userStoryID);

        // Select the right project and release
        ReleaseSelectionSprint.SetProjectReleaseBySprint(sprint.ID);

        LoadSprints();

        // Select the right sprint
        for (int i = 0; i < this.DropDownListSprints.Items.Count; i++)
        {
            if (this.DropDownListSprints.Items[i].Value == sprint.ID.ToString())
            {
                this.DropDownListSprints.SelectedIndex = i;
                break;
            }
        }
    }
        
    /// <summary>
    /// Clears the dropdown lists.
    /// </summary>
    public void Clear(object sender, EventArgs e)
    {
        this.ReleaseSelectionSprint.Clear(sender, e);
        //DataBind();
        //this.DropDownListProject.SelectedIndex = 0;
        //this.DropDownListTeams.SelectedIndex = 0;
    }

    protected override WebControl GetWebControl()
    {
        return this.DropDownListSprints;
    }

    protected override Label GetLabel()
    {
        return this.LabelSprints;
    }
}
